// student.js
const studentApp = createApp({
    data() {
        return {
            students: [],
            loading: false,
            error: null,
            formData: {
                userNo: '',
                name: '',
                enrollmentYear: '',
                collegeCode: '',
                majorCode: '',
                classCode: ''
            }
        }
    },
    methods: {
        // 加载学生数据
        async loadStudents() {
            this.loading = true;
            this.error = null;
            try {
                const response = await fetch(window.contextPath+'/student/listAll');
                if (!response.ok) {
                    this.error = 'HTTP error! status:'+response.status;
                    console.error('HTTP错误:', response.status);
                    return;
                }
                this.students = await response.json();
            } catch (error) {
                this.error = error.message;
                console.error('加载学生数据失败:', error);
            } finally {
                this.loading = false;
            }
        },
        // 添加学生
        async addStudent() {
            if (!this.formData.userNo || this.formData.userNo.length !== 10) {
                this.error = '学号必须为10位数字';
                showErrorModal(this.error);
                return;
            }

            try {
                const response = await fetch(window.contextPath+'/student/add', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(this.formData)
                });

                if (response.ok) {
                    alert('添加成功')
                    await this.loadStudents();
                    this.formData = {
                        userNo: '',
                        name: '',
                        enrollmentYear: '',
                        collegeCode: '',
                        majorCode: '',
                        classCode: ''
                    };
                } else {
                    const errorText = await response.text();
                    this.error = '添加学生失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 从学号中提取信息
        async extractInfoFromUserNo() {
            const userNo = this.formData.userNo;
            if (userNo && userNo.length === 10) {
                this.formData.enrollmentYear = userNo.substring(0, 2);
                this.formData.collegeCode = userNo.substring(2, 4);
                this.formData.majorCode = userNo.substring(4, 6);
                this.formData.classCode = userNo.substring(6, 8);
            } else {
                this.formData.enrollmentYear = '';
                this.formData.collegeCode = '';
                this.formData.majorCode = '';
                this.formData.classCode = '';
            }
        },
        // 删除学生
        async deleteStudent(id) {
            try {
                if (!confirm('确定要删除该学生吗？这将造成不可挽回的后果，有关该学生的其他信息(如成绩信息)均会被删除,如无实在需求不建议删除。')) {
                    return;
                }
                const response = await fetch(window.contextPath+'/student/delete?id=' + id, {
                    method: 'DELETE'
                });
                if (response.ok) {
                    alert('删除成功')
                    await this.loadStudents();
                } else {
                    const errorText = await response.text();
                    this.error = '删除学生失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 修改学生信息
        async updateStudent() {
            alert('该功能正在开发中...')
        }
    },
    mounted() {
        window.studentAppInstance = this;
    }
});
studentApp.mount('#student-content');

// 显示添加学生表单
function showStudentAddModal() {
    const modal = document.getElementById('studentAddModal');
    modal.classList.add('show');
    modal.style.display = 'flex';
}

// 关闭添加学生表单
function closeStudentAddModal() {
    document.getElementById('studentAddModal').style.display = 'none';
}
